#pragma once

#include <thread>
#include <mutex>
#include <condition_variable>
#include "event_loop.h"

namespace rr {
namespace nio {

class EventLoopThread
{
public:
    using Sptr = std::shared_ptr<EventLoopThread>;
    using EventLoopCallback = std::function<void(EventLoop::Sptr)>;

public:
    EventLoopThread(const std::string& name = "");
    ~EventLoopThread();

    void SetInitCallback(const EventLoopCallback& cb) { init_cb_ = cb; }
    void SetExitCallback(const EventLoopCallback& cb) { exit_cb_ = cb; }

    int32_t StartLoop();
    void StopLoop();

    EventLoop::Sptr GetLoop() { return loop_; }

private:
    void ThreadFunc();

private:
    std::shared_ptr<std::thread> thread_;
    EventLoopCallback init_cb_ = nullptr;
    EventLoopCallback exit_cb_ = nullptr;

    EventLoop::Sptr loop_ = nullptr;
    std::mutex mutex_;
    std::condition_variable cond_;
};

} //namespace 
} //namespace 
